package com.tim.app.ui.activity.person_center;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.activity.selected.PublishDynamicActivity;
import com.tim.app.ui.fragment.someone_homepage.PersonalDynamicFragment;
import com.tim.app.ui.fragment.someone_homepage.PersonalSayFragment;


/**
 * 某人的动态页面
 */
public class SomeOneDynamicActivity extends BaseActivity {

    public static final String TAG = "SomeOneDynamicActivity";

    private ImageButton ibBack;
    private TextView tvTitle;
    private ImageView ivPostDynamic;

    private FrameLayout flContainer;
    private User currentUser;

    private boolean isDynamic = false;

    public static void start(Context context, User user, boolean isDyanmic) {
        Intent intent = new Intent(context, SomeOneDynamicActivity.class);
        intent.putExtra(AppKey.USER, user);
        intent.putExtra("isDyanmic", isDyanmic);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_dynamic;
    }


    @Override
    public void initView() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        ivPostDynamic = (ImageView) findViewById(R.id.ivPostDynamic);

        flContainer = (FrameLayout) findViewById(R.id.flContainer);

        ibBack.setOnClickListener(this);
        ivPostDynamic.setOnClickListener(this);

        currentUser = (User) getIntent().getSerializableExtra(AppKey.USER);
        isDynamic = getIntent().getBooleanExtra("isDyanmic", false);

        if (null != currentUser && !TextUtils.isEmpty(currentUser.getUserId()) && UserManager.ins().isLogin() && UserManager.ins().getUserId().equals(currentUser.getUserId())) {
            if (isDynamic) {
                ivPostDynamic.setVisibility(View.VISIBLE);
                tvTitle.setText("我的说说");
            } else {
                ivPostDynamic.setVisibility(View.GONE);
                tvTitle.setText("我的动态");
            }
        } else {
            ivPostDynamic.setVisibility(View.GONE);
            tvTitle.setText(currentUser.getName() + "的说说");
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment;
        if (isDynamic) {
            fragment = PersonalSayFragment.newInstance(currentUser);
        } else {
            fragment = PersonalDynamicFragment.newInstance(currentUser);
        }
        transaction.replace(R.id.flContainer, fragment);
        transaction.commit();
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibBack://返回
                finish();
                break;
            case R.id.ivPostDynamic:
                Intent intent = new Intent(this, PublishDynamicActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

}
